/*
 * @Author: sichuntian sichuntian@smartmappingtek.com
 * @Date: 2023-11-28 16:58:07
 * @LastEditors: sichuntian sichuntian@smartmappingtek.com
 * @LastEditTime: 2024-05-16 16:22:39
 * @Description:
 *
 */
const { signModel, routeLineModel, vectorLineModel } = require("../../models/common")

// 保存路牌信息
async function saveSign(body) {
  try {
    const { name, ...data } = body
    const res = await signModel.find({ name: name })
    if (res.length > 0) {
      return {
        code: 3000,
        message: "添加失败，当前路牌已存在",
        result: [],
      }
    }
    const params = { name, ...data, createTime: new Date().getTime() }
    const result = await signModel.create(params)
    return {
      code: 200,
      message: "添加成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 保存路牌信息
async function getSignList(body) {
  try {
    const result = await signModel.find()
    return {
      code: 200,
      message: "获取成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 删除路牌信息
async function delSign(body) {
  try {
    let { _id } = body
    let results = await signModel.findByIdAndDelete(_id)
    return { code: 200, message: "删除成功", results: [results] }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 保存矢量路线
async function saveVectorLine(body) {
  try {
    const { name, ...data } = body
    const res = await vectorLineModel.find({ name: name })
    if (res.length > 0) {
      return {
        code: 3000,
        message: "添加失败，当前路线已存在",
        result: [],
      }
    }
    const params = { name, ...data, createTime: new Date().getTime() }
    const result = await vectorLineModel.create(params)
    return {
      code: 200,
      message: "添加成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 保存漫游路线
async function saveRouteLine(body) {
  try {
    const { name, ...data } = body
    const res = await routeLineModel.find({ name: name })
    if (res.length > 0) {
      return {
        code: 3000,
        message: "添加失败，当前路线已存在",
        result: [],
      }
    }
    const params = { name, ...data, createTime: new Date().getTime() }
    const result = await routeLineModel.create(params)
    return {
      code: 200,
      message: "添加成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 获取全部漫游路线
async function getAllRouteLines(body) {
  try {
    const result = await routeLineModel.find()
    return {
      code: 200,
      message: "获取成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}
// 获取全部矢量路线
async function getAllVectorLines(body) {
  try {
    const result = await vectorLineModel.find()
    return {
      code: 200,
      message: "获取成功",
      result: result,
    }
  } catch (err) {
    return { code: 500, result: err, message: "服务异常" }
  }
}

module.exports = {
  saveSign,
  saveVectorLine,
  saveRouteLine,
  getAllRouteLines,
  getAllVectorLines,
  getSignList,
  delSign,
}
